﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;

namespace CarInfor.UI
{
    public partial class ctlYearCheckAlert : UserControl
    {
        DateTimePicker dtpStart = null;
        DateTimePicker dtpEnd = null;

        NumericUpDown numYear = null;
        NumericUpDown numMouth = null;

        //当前查询条件
        string curConSql = string.Empty;
        DataSet curDataSet = new DataSet();

        public ctlYearCheckAlert()
        {
            InitializeComponent();
            Init();
            InitData();
        }
        /// <summary>
        /// 刷新界面
        /// </summary>
        public void RefreshUI()
        {
            InitData();
            if (!string.IsNullOrEmpty(this.dtpStart.Text) && !string.IsNullOrEmpty(this.dtpEnd.Text))
            {
                string sqlstr = " ExpireDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
                this.curConSql = sqlstr;
                Bind(1, false);
            }
            else
            {
                this.curConSql = string.Empty;
                Bind(1, false);
            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            AlertSetting ast = new AlertSetting();
            ast.GetSetting();
            this.dtpStart.Value = DateTime.Now.AddDays(ast.YearCheckAlertB * (-1));
            this.dtpEnd.Value = DateTime.Now.AddDays(ast.YearCheckAlertE);
            numYear.Value = DateTime.Now.Year;
            this.numMouth.Value = DateTime.Now.Month;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        void Init()
        {
            Common.SetGridViewStyle(this.dataGridView1);

            numYear = new NumericUpDown();
            numYear.Maximum = 9999;
            numYear.Minimum = 1900;
            numYear.AutoSize = false;
            this.toolStrip1.Items.Insert(3, new ToolStripControlHost(numYear));

            numMouth = new NumericUpDown();
            numMouth.Maximum = 12;
            numMouth.Minimum = 1;
            numMouth.AutoSize = false;
            this.toolStrip1.Items.Insert(5, new ToolStripControlHost(numMouth));

            dtpStart = new DateTimePicker();
            dtpStart.Format = DateTimePickerFormat.Short;
            dtpStart.Width = 100;
            dtpEnd = new DateTimePicker();
            dtpEnd.Format = DateTimePickerFormat.Short;
            dtpEnd.Width = 100;
            this.toolStrip1.Items.Insert(9, new ToolStripControlHost(dtpStart));
            ToolStripLabel lbl = new ToolStripLabel();
            lbl.Text = "到";
            this.toolStrip1.Items.Insert(10, lbl);
            this.toolStrip1.Items.Insert(11, new ToolStripControlHost(dtpEnd));

            this.ctlPager1.PageSize = MyGlobal.PageSize;
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void Bind(int pageIndex, bool isPageEvent)
        {
            YearCheck yearCheck = new YearCheck();
            DataTable dt = null;
            dt = yearCheck.Query(curConSql);

            //计算分页信息
            int startPos = (pageIndex - 1) * this.ctlPager1.PageSize;
            int count = this.ctlPager1.PageSize;
            if ((pageIndex - 1) * ctlPager1.PageSize + ctlPager1.PageSize > dt.Rows.Count)
            {
                count = dt.Rows.Count - startPos;
            }
            int total = dt.Rows.Count;

            //筛选分页数据
            Common.GetRange(dt, startPos, count);
            if (!isPageEvent)
            {
                this.ctlPager1.Initialize(total);
            }
            this.dataGridView1.DataSource = dt;
            curDataSet.Tables.Clear();
            curDataSet.Tables.Add(dt.Copy());
            dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.Columns[0].FillWeight = 5;
            this.dataGridView1.Columns["编号"].Visible = false;
            this.dataGridView1.Columns["序号"].FillWeight = 5;
            this.dataGridView1.Columns["车辆号牌"].FillWeight = 9;
            this.dataGridView1.Columns["车辆类型"].FillWeight = 9;
            this.dataGridView1.Columns["车主姓名"].FillWeight = 9;
            this.dataGridView1.Columns["联系电话"].FillWeight = 9;
            this.dataGridView1.Columns["年检日期"].FillWeight = 10;
            this.dataGridView1.Columns["到期日期"].FillWeight = 10;
            this.dataGridView1.Columns["报户日期"].FillWeight = 10;

            this.dataGridView1.Columns["备注"].FillWeight = 24;

            this.dataGridView1.Columns["车辆号牌"].ReadOnly = true;
            this.dataGridView1.Columns["车辆类型"].ReadOnly = true;
            this.dataGridView1.Columns["车主姓名"].ReadOnly = true;
            this.dataGridView1.Columns["联系电话"].ReadOnly = true;
            this.dataGridView1.Columns["年检日期"].ReadOnly = true;
            this.dataGridView1.Columns["到期日期"].ReadOnly = true;
            this.dataGridView1.Columns["备注"].ReadOnly = true;

            this.dataGridView1.Columns["年检日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["到期日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["报户日期"].DefaultCellStyle.Format = "yyyy-MM-dd";

            this.chkSelectAll.Checked = false;
        }

        private void ctlYearCheckAlert_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.dtpStart.Text) && !string.IsNullOrEmpty(this.dtpEnd.Text))
            {
                string sqlstr = " ExpireDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
                this.curConSql = sqlstr;
                Bind(1, false);
            }
            else
            {
                this.curConSql = string.Empty;
                Bind(1, false);
            }
        }

        /// <summary>
        /// 按到期日期查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnQuery_Click(object sender, EventArgs e)
        {
            string sqlstr = " ExpireDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
            this.curConSql = sqlstr;
            Bind(1, false);
        }

        /// <summary>
        /// 按月查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueryByMonth_Click(object sender, EventArgs e)
        {
            string sqlstr = "  year(ExpireDate)=" + this.numYear.Value + "  and  month(ExpireDate)=" + this.numMouth.Value;
            this.curConSql = sqlstr;
            Bind(1, false);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnEdit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count != 0)
            {
                string id = this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString();
                frmCarInfor carInfor = new frmCarInfor(ActionType.Edit, id);
                if (carInfor.ShowDialog() == DialogResult.OK)
                {
                    string sqlstr = " ExpireDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
                    this.curConSql = sqlstr;
                    Bind(1, false);
                }
            }
        }

        /// <summary>
        /// 列表双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > 0)
            {
                if (this.dataGridView1.SelectedRows.Count != 0)
                {
                    string id = this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString();
                    frmCarInfor carInfor = new frmCarInfor(ActionType.Edit, id);
                    if (carInfor.ShowDialog() == DialogResult.OK)
                    {
                        string sqlstr = " ExpireDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
                        this.curConSql = sqlstr;
                        Bind(1, false);
                    }
                }
            }
        }

        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkSelectAll.Checked)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.EndEdit();
                    DataGridViewCheckBoxCell checkEditCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["chkSelect"];
                    checkEditCell.Value = true;
                }
            }
            else
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.EndEdit();
                    DataGridViewCheckBoxCell checkEditCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["chkSelect"];
                    checkEditCell.Value = false;
                }
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnPrint_Click(object sender, EventArgs e)
        {
            RDLCPrint.ReportViewer frmRPT = new RDLCPrint.ReportViewer();
            frmRPT.MainDataSet = curDataSet;
            frmRPT.ReportName = "Test";
            frmRPT.MainDataSourceName = "dbDataSet_YearCheckForReport";
            frmRPT.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Report\YearCheck.rdlc";
            frmRPT.ShowDialog();
        }

        /// <summary>
        /// 获取用户选择的数据
        /// </summary>
        /// <returns></returns>
        private string GetSelectRecord()
        {
            string ids = string.Empty;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.EndEdit();
                DataGridViewCheckBoxCell checkEditCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["chkSelect"];
                Boolean flag = Convert.ToBoolean(checkEditCell.Value);
                if (flag)
                {
                    ids += "'" + this.dataGridView1.Rows[i].Cells["编号"].Value.ToString() + "',";
                }
            }
            return ids.TrimEnd(',');
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendMsg_Click(object sender, EventArgs e)
        {
            string rids = GetSelectRecord();
            if (string.IsNullOrEmpty(rids))
            {
                MessageHelper.ShowInfo("请选择要发送短信的客户！");
                return;
            }
            frmSendMsg msg = new frmSendMsg(MsgType.Yearcheck, rids);
            msg.ShowDialog();
        }

        /// <summary>
        /// 翻页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void ctlPager1_PageChanged(object sender, Controls.PagerArgs args)
        {
            Bind(args.PageIndex, true);
        }
    }
}
